package com.aliyun.messagemgt.converter;

import com.alibaba.fastjson.JSON;
import com.aliyun.messagemgt.common.dto.send.MsgBody;
import com.aliyun.messagemgt.common.dto.send.MsgRecordVO;
import com.aliyun.messagemgt.dto.MessageRecord;
import com.aliyun.messagemgt.dto.msg.MessageRecordDTO;
import com.aliyun.messagemgt.entities.MessageRecordEntity;
import com.github.pagehelper.PageInfo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @ClassName: MessageRecordConverter
 * @Description:
 * @Author: wugongzi
 * @Date: 2021/9/13 18:17
 * @Version: 1.0
 */
@Mapper
public interface MessageRecordConverter {
    MessageRecordConverter INSTANCE = Mappers.getMapper(MessageRecordConverter.class);

    List<MessageRecord> messageRecordEntityList2messageRecordList(List<MessageRecordEntity> messageRecordEntityList);

    List<MessageRecordEntity> msgRecordVoList2MessageRecordEntityList(List<MsgRecordVO> msgRecordVos);

    @Mappings({
            @Mapping(expression = "java(com.alibaba.fastjson.JSON.toJSONString(msgRecordVO.getContents()))", target = "content"),
    })
    MessageRecordEntity msgRecordVo2MessageRecordEntity(MsgRecordVO msgRecordVO);

    List<MessageRecordDTO> messageRecordEntityList2messageRecordDTOList(List<MessageRecordEntity> messageRecordEntityList);

    PageInfo<MessageRecordDTO> messageRecordEntityPageInfo2messageRecordDTOPageInfo(PageInfo<MessageRecordEntity> messageRecordEntityPageInfo);

    @Mappings({
            @Mapping(source = "timeCreate", target = "createTime"),
            @Mapping(expression = "java(messageRecordEntity.getStatus() == null || messageRecordEntity.getStatus() == false?\"失败\":\"成功\")", target = "status"),
    })
    MessageRecordDTO messageRecordEntity2messageRecordDTO(MessageRecordEntity messageRecordEntity);


    default List<MsgBody> processContent(String content) {
        return JSON.parseArray(content, MsgBody.class);
    }

    @Mappings({
            @Mapping(expression = "java(processContent(messageRecord.getContent()))", target = "contents"),
            @Mapping(source = "msgTitle", target = "subject"),
    })
    MsgRecordVO messageRecord2msgRecordVO(MessageRecordEntity messageRecord);
}
